package cn.elwy.rcp.kit.builder.decode;

import java.io.File;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.eclipse.e4.ui.di.Focus;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;

public class DecodePart {
	private Text srcText;
	private Text destText;
	private Button srcButton;
	private Button destButton;
	private Text recoverText;
	private Text filterExtNameText;
	private Button filterExtNameButton;

	public DecodePart() {
	}

	/**
	 * Create contents of the view part.
	 */
	@PostConstruct
	public void createControls(final Composite parent) {

		TabFolder tabFolder = new TabFolder(parent, SWT.NONE);

		// tabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		TabItem backTabItem = new TabItem(tabFolder, SWT.NONE);
		backTabItem.setText("备份");

		Composite backComposite = new Composite(tabFolder, SWT.NONE);
		backTabItem.setControl(backComposite);
		backComposite.setLayout(new GridLayout(3, false));

		Label lblNewLabel = new Label(backComposite, SWT.NONE);
		lblNewLabel.setSize(48, 17);
		lblNewLabel.setText("源目录：");

		srcText = new Text(backComposite, SWT.BORDER);
		srcText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		srcText.setSize(294, 23);

		srcButton = new Button(backComposite, SWT.NONE);
		srcButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String filePath = openDirectoryDialog(parent.getShell());
				srcText.setText(filePath);
			}
		});
		srcButton.setText("选择目录");

		Label lblNewLabel_1 = new Label(backComposite, SWT.NONE);
		lblNewLabel_1.setText("备份目录：");

		destText = new Text(backComposite, SWT.BORDER);
		destText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		destButton = new Button(backComposite, SWT.NONE);
		destButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String filePath = openDirectoryDialog(parent.getShell());
				destText.setText(filePath);
			}
		});
		destButton.setText("选择目录");

		filterExtNameButton = new Button(backComposite, SWT.CHECK);
		filterExtNameButton.setSelection(true);
		filterExtNameButton.setText("过滤文件");

		filterExtNameText = new Text(backComposite, SWT.BORDER | SWT.WRAP | SWT.MULTI);
		filterExtNameText
				.setText(".*\\.java$|.*\\.cs$|.*\\.resx$|.*\\.doc$|.*\\.docx$|.*\\.xls$|.*\\.xlsx$|.*\\.ppt$|.*\\.pptx$");
		GridData gd_filterExtNameText = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		gd_filterExtNameText.heightHint = 60;
		filterExtNameText.setLayoutData(gd_filterExtNameText);
		new Label(backComposite, SWT.NONE);

		Button okButton = new Button(backComposite, SWT.NONE);
		okButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		okButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent event) {
				String srcPath = srcText.getText();
				String destPath = destText.getText();
				try {
					File srcFile = new File(srcPath);
					if (!srcFile.exists()) {
						throw new RuntimeException(srcPath + "不存在！");
					}
					File destFile = new File(destPath);
					if (destFile.isFile()) {
						throw new RuntimeException(destPath + "不可以是文件！");
					}
					String includeExtNames = null;
					if (filterExtNameButton.getSelection()) {
						includeExtNames = filterExtNameText.getText();
					}
					Decryptor.copyFiles(srcFile, destFile, "", includeExtNames);
					MessageDialog.openInformation(parent.getShell(), "信息", "文件输出成功！");
					return;
				} catch (Exception e) {
					MessageDialog.openError(parent.getShell(), "错误", e.getMessage());
					return;
				}
			}
		});
		okButton.setText("确认");
		new Label(backComposite, SWT.NONE);

		TabItem recoverTabItem = new TabItem(tabFolder, SWT.NONE);
		recoverTabItem.setText("恢复");

		Composite composite = new Composite(tabFolder, SWT.NONE);
		recoverTabItem.setControl(composite);
		composite.setLayout(new GridLayout(3, false));

		Label label = new Label(composite, SWT.NONE);
		label.setText("源目录：");

		recoverText = new Text(composite, SWT.BORDER);
		recoverText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Button button = new Button(composite, SWT.NONE);
		button.setSize(60, 27);
		button.setText("选择目录");
		new Label(composite, SWT.NONE);

		Button button_1 = new Button(composite, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent event) {
				String srcPath = recoverText.getText();
				try {
					File srcFile = new File(srcPath);
					if (!srcFile.exists()) {
						throw new RuntimeException(srcPath + "不存在！");
					}

					Decryptor.renameTo(srcFile);
					MessageDialog.openInformation(parent.getShell(), "错误", "文件输出成功！");
					return;
				} catch (Exception e) {
					MessageDialog.openError(parent.getShell(), "错误", e.getMessage());
					return;
				}

			}
		});
		button_1.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		button_1.setText("确认");
		button_1.setBounds(0, 0, 36, 27);
		new Label(composite, SWT.NONE);

	}

	protected String openDirectoryDialog(Shell parent) {
		// 新建文件对话框，并设置为打开的方式
		DirectoryDialog dialog = new DirectoryDialog(parent);
		// 设置文件对话框的标题
		dialog.setText("选择目录");
		// 设置初始路径
		dialog.setFilterPath("SystemDrive");
		// 设置对话框提示文本信息
		dialog.setMessage("请选择相应的文件夹");
		// 打开文件对话框，返回选中文件的绝对路径
		String selected = dialog.open();
		return selected == null ? "" : selected;
	}

	protected String openFileDialog(Shell parent) {
		// 新建文件对话框，并设置为打开的方式
		FileDialog dialog = new FileDialog(parent, SWT.OPEN);
		// 设置文件对话框的标题
		dialog.setText("文件选择");
		// 设置初始路径
		dialog.setFilterPath("SystemRoot");
		// 打开文件对话框，返回选中文件的绝对路径
		// 设置对话框提示文本信息
		String selected = dialog.open();
		return selected == null ? "" : selected;
	}

	public static String getExtName(String fileName) {
		int index = fileName.lastIndexOf(".");
		return index != -1 ? fileName.substring(index + 1) : "";
	}

	@PreDestroy
	public void dispose() {
	}

	@Focus
	public void setFocus() {
	}
}
